import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Controller extends JFrame {
	JPanel p = new JPanel();
	JButton btnRun = new JButton("Run");
	JButton btnStop = new JButton("Stop");
	Timer timer;
	Model_Model model = new Model_Model();
	View view = new View(model);
	String[] mau = { "Red", "Green", "Blue", "Magenta", "Yello" };
	JComboBox jcb = new JComboBox(mau);
	JButton btnSubmit = new JButton("Submit");

	public void init() {
		layOutComponent();
		attachListenerToComponent();
		model.addObserver(view);

	}

	private void layOutComponent() {
		setLayout(new BorderLayout());
		this.add(BorderLayout.SOUTH, p);
		p.add(btnRun);
		p.add(btnStop);
		p.add(jcb);
		p.add(btnSubmit);
		// btnSubmit.setEnabled(false);
		btnStop.setEnabled(false);
		this.add(BorderLayout.CENTER, view);
	}

	private void attachListenerToComponent() {
		btnRun.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				btnRun.setEnabled(false);
				btnStop.setEnabled(true);
				timer = new Timer(true);
				timer.schedule(new Strobe(), 0, 50);
			}
		});
		btnStop.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				btnRun.setEnabled(true);
				btnStop.setEnabled(false);
				timer.cancel();

			}
		});
		btnSubmit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				String mau = jcb.getSelectedItem().toString();
			}
		});

	}

	// @Override
	private class Strobe extends TimerTask {
		@Override
		public void run() {
			model.setLimit(view.getWidth(), view.getHeight());
			model.make_makeOneStep();

		}
	}

	public static void main(String[] args) {
		Controller controllerApp = new Controller();
		controllerApp.init();
		controllerApp.setSize(400, 300);
		controllerApp.setVisible(true);
		controllerApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}
